package com.yanggu.spark.core.rdd.transform.value

import org.apache.spark.{SparkConf, SparkContext}

//repartition算子
object RDD10_Repartition {

  def main(args: Array[String]): Unit = {

    //1. 创建sparkConf配置对象
    val sparkConf = new SparkConf().setMaster("local[*]").setAppName("spark")

    //2. 创建spark上下文对象
    val sparkContext = new SparkContext(sparkConf)

    //3. 从内存中创建RDD
    val dataRdd = sparkContext.makeRDD(List[Int](1, 2, 3, 3 ,4, 5, 6), 6)

    //4. repartition
    //底层直接调用了coalesce(numPartitions, shuffle = true)方法。该方法可以提高或者降低RDD的并行度
    val value = dataRdd.repartition(4)
    println("新的分区数: " + value.getNumPartitions)

    //5. 打印
    println(value.collect().mkString(", "))

    //6. 释放资源
    sparkContext.stop()
  }

}
